# -*- coding: utf-8 -*-

# project - description
# Copyright (C) 2010  Cédric Bonhomme - http://cedricbonhomme.org/
#
# For more information : http://bitbucket.org/cedricbonhomme/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2011/02/25 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"

import time
import math
import android

import urllib

def getRate(speed):
    """
    This function tell us how often to do reports.
    """
    if speed < 5:  # 5 mph is first threshod
        rate=1800    # under 5 mph, rate will be 30 min or 1800 seconds
    else:
        if speed < 40: # 40 mph is second threshold
            rate = 600 # over 5, but under 40 we'll set the rate to 10 min
        else:
            rate = 120 # over 40 mph well set the rate at 2 min.
    return rate

def change_in_speed(old, new):
    """
    Decides if we need to record a position because
    our speed has changed in some important way.
    """
    false = 0
    true = 1
    beacon = false
    if (old == 0 and new <> 0):
        beacon = true
    if (new == 0 and old <> 0):
        beacon = true
    if (abs(old - new) > 20):
        beacon = true
    if (old < 5 < new):
        beacon = true
    if (old < 40) and (40 < new):
        beacon = true
    if (new < 5) and (5 < old):
        beacon = true
    if (new < 40) and (40 < old):
        beacon = true
    return beacon

def getBearing(deltaLat, deltaLon):
    """
    Return the bearing.
    """
    theta = math.atan2(deltaLat,deltaLon) * (180/math.pi)
    bearing = int((450 - theta ) % 360)
    return bearing

def change_in_bearing(old, new):
    """
    Lets us know if we have changed direction by more
    than 15 degrees since our last bearing report.
    """
    false = 0
    true = 1
    if old < new:
        lesser = old
        greater = new
    else:
        lesser = new
        greater = old
    difference = greater - lesser
    beacon = false
    if (15 < difference < 345):
        beacon = true
    return beacon

if __name__ == "__main__":
    # Point of entry in execution
    print "-= WiSafeCar client =-"

    droid = android.Android()
    print "Starting localization..."
    droid.startLocating()

    time.sleep(10)

    l = droid.readLocation().result

    if l != {}:
        if 'gps' in l:
            location, provider = l['gps'], 'gps'
        elif 'network' in l:
            location, provider = l['network'], 'network'

        latitude = location['latitude']
        longitude = location['longitude']
        altitude = location['altitude']
        utime = location['time']
        speed = location['speed']

        print "Latitude, longitude:", latitude, ",", longitude
        print "Altitude:", altitude
        print "Time, speed:", utime, speed
        print "Provider (GPS or Network):", provider
        print "-" * 20
        adr = 'http://where-is.cedricbonhomme.org/checkin.cgi?latitude=' + \
                str(latitude) + '&longitude=' + str(longitude) + '&utime=' + str(time.time())
        try:
            urllib.urlopen(adr)
        except Exception, e:
            print e